/*
Class Name      : Fileattachementclass
Created by      : Suneetha Allam Venkata
Created On      : 19/02/2013
Description     : This class helps to save attachements for the account
                   
Version         : 1.0         
*/
public  class  Fileattachementclass {

public integer filesize  {get; set;}
public  transient blob attachmentFile {get;set;}
public String attachmentFilename {get; set;}
public boolean  isactive {get; set;}
public Attachment  newAttachment{get; set;}


public Fileattachementclass() {
     isactive =false;
    }
       
  public boolean del()
    
    {
     delete newAttachment;
                
    return  true;
    }
  public boolean attach()
    
    {
         /****If there is an attachment, it is saved****/
            if(attachmentFilename!=null && attachmentFilename!=''){
           
            if(filesize>1048576)
            {
        
             ApexPages.addMessage(new ApexPages.Message(ApexPages.severity.WARNING,'File size is exceed, Please select attach File not More than 1 MB'));
              isactive=false;
              return false;
            }
             //ApexPages.addMessage(new ApexPages.Message(ApexPages.severity.WARNING,'FILE NAME'+ attachmentFilename +' FILE' + attachmentFile));
                 newAttachment = new Attachment();
               // newAttachment.parentid=Taskid;
                 newAttachment.Name=attachmentFilename;
                 newAttachment.Body=attachmentFile;
              //  insert newAttachment;
                
            }
            return true;
        
    }
    
    //helps to save an  attachement
    public  boolean save(case newCase)
    {
      try{
    // ApexPages.addMessage(new ApexPages.Message(ApexPages.severity.ERROR,newAttachment.Name)); 
            if ( isactive==true)
            {
                Attachment t1=new Attachment();
                t1.ParentId=newCase.id;
                t1.Name=newAttachment.Name;
                t1.Body=  newAttachment.Body;
                upsert t1;
                  // ApexPages.addMessage(new ApexPages.Message(ApexPages.severity.ERROR,newAttachment.Name)); 
               
           
            }
         
             }
                catch(DmlException dmle){
                   
                    //ApexPages.addMessage(new ApexPages.Message(ApexPages.severity.ERROR,'Unable to upload attachment'));
                    return false;
                }
                   return true;
    }
    
    public boolean validate()
    {
        //added By suneetha 16 feb 2013( For file size validation);
        //    Maximum view state size limit (135KB) exceeded. Actual view state size for this page was 4,166.875KB 
        //   ApexPages.addMessage(new ApexPages.Message(ApexPages.severity.ERROR, 'filename:' +attachmentFilename));
        //ApexPages.addMessage(new ApexPages.Message(ApexPages.severity.ERROR, 'filesize:' +filesize));
         //138240  --  (135Kb), 5242880 --- (5MB)
         //return null;
        if(filesize>1048576)
        {
        
              ApexPages.addMessage(new ApexPages.Message(ApexPages.severity.WARNING, 'Please select file less than 1 MB') );
              return false;
    
    
        }
        return true;
          //ended by suneetha 16 feb 2013
    }
    
    }